//
//  NSString+Validation.m
//  BaseLib
//
//  Created by CPX on 2018/7/23.
//

#import "NSString+Helper.h"

/**
 String帮助类
 @author yzl
 */
@implementation NSString (Helper)

- (BOOL)validateEmail
{
    if (self.length <= 0) {
        return NO;
    }
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:self];
}

- (BOOL)isPureInt{
    
    NSScanner* scan = [NSScanner scannerWithString:self];
    
    int val;
    
    return [scan scanInt:&val] && [scan isAtEnd];
}

- (NSString*)arabicString{
    NSMutableString *number = [NSMutableString stringWithString:self];
    [number replaceOccurrencesOfString:@"0" withString:@"٠" options:NSCaseInsensitiveSearch range:NSMakeRange(0, number.length)];
    [number replaceOccurrencesOfString:@"1" withString:@"١" options:NSCaseInsensitiveSearch range:NSMakeRange(0, number.length)];
    [number replaceOccurrencesOfString:@"2" withString:@"٢" options:NSCaseInsensitiveSearch range:NSMakeRange(0, number.length)];
    [number replaceOccurrencesOfString:@"3" withString:@"٣" options:NSCaseInsensitiveSearch range:NSMakeRange(0, number.length)];
    [number replaceOccurrencesOfString:@"4" withString:@"٤" options:NSCaseInsensitiveSearch range:NSMakeRange(0, number.length)];
    [number replaceOccurrencesOfString:@"5" withString:@"٥" options:NSCaseInsensitiveSearch range:NSMakeRange(0, number.length)];
    [number replaceOccurrencesOfString:@"6" withString:@"٦" options:NSCaseInsensitiveSearch range:NSMakeRange(0, number.length)];
    [number replaceOccurrencesOfString:@"7" withString:@"٧" options:NSCaseInsensitiveSearch range:NSMakeRange(0, number.length)];
    [number replaceOccurrencesOfString:@"8" withString:@"٨" options:NSCaseInsensitiveSearch range:NSMakeRange(0, number.length)];
    [number replaceOccurrencesOfString:@"9" withString:@"٩" options:NSCaseInsensitiveSearch range:NSMakeRange(0, number.length)];
    return number;
}

-(NSString *)filterHTML
{
    NSString *html = self;
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text = nil;
    while([scanner isAtEnd]==NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"<" intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:@">" intoString:&text];
        //替换字符
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    }
    //    NSString * regEx = @"<([^>]*)>";
    //    html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
    return html;
}

@end
